package com.zsj.job;

import com.zsj.config.ConfigBean;
import com.zsj.config.ScheduledCondition;
import com.zsj.service.BatchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

@Slf4j
@Component
@Conditional(ScheduledCondition.class)
public class CreateFileJob {

    @Autowired
    private ConfigBean configBean;

    @Autowired
    private BatchService batchService;

    @Scheduled(cron = "0 10 0 * * ?")
    public void createFile() {
        if (!"true".equals(configBean.getBatchSwitch())) {
            log.info("批处理开关为:{}，不执行批处理任务", configBean.getBatchSwitch());
            return;
        }
        LocalDate lastDay = LocalDate.now().minusDays(1);
        log.info("开始执行生成对账文件任务，日期:{}", lastDay.toString());
        try {
            boolean flag = batchService.createFile(lastDay.toString().replace("-", ""));
            if (flag) {
                log.info("生成{}对账文件成功", lastDay.toString());
            } else {
                log.info("生成{}对账文件失败", lastDay.toString());
            }
        } catch (Exception e) {
            log.error("生成对账文件异常", e);
        }
        log.info("生成{}的对账文件任务结束", lastDay.toString());
    }

    //@Scheduled(cron = "0 */5 23 * * ?")
    // public void testJob() {
    //    if (!"true".equals(batchSwitch)) {
    //        log.info("批处理开关为:{}，不执行批处理任务", batchSwitch);
    //        return;
    //    } else {
    //        log.info("批处理开关为:{}，执行批处理任务", batchSwitch);
    //        return;
    //    }
    //}
}
